﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Tools;

namespace Pixysoft.Weblications.Sango.Formular
{
    class EquipmentFormular
    {
        /** 成功率维持时间段 */
        private static int successrate_span = 30;

        /** 成功率按照30分钟变换一次，总周期（计算出周期时间=4小时） */
        private static int successrate_period = 8;

        /** 成功率开始基数 */
        private static int successrate_start_delta = 20;

        /** 最大成功率 */
        public static int default_maxSuccessRate = 100;

        //无装备的序列号
        public static int default_equipment_none = -1;

        public const int default_eqm_buffertimespan = -1;

        /** 队列冷却时间 */
        public static int default_queue_cooltimespan = DateTimeCoder.Instance.MinuteToMilliSecond(5);

        /// <summary>
        /// 装备价格
        /// </summary>
        /// <param name="eqm"></param>
        /// <returns></returns>
        public static long getEquipmentCashprice(Pixysoft.Weblications.Sango.Entities.Eqm_Equipment eqm)
        {
            return eqm.Equipmentprice;
        }

        /// <summary>
        /// 装备升级价格
        /// </summary>
        /// <param name="eqm"></param>
        /// <returns></returns>
        public static long getUpgradePrice(Entities.Usr_Equipment eqm)
        {
            //y = 51x+100

            return 51 * eqm.Gradelevel + 100;
        }

        /// <summary>
        /// 装备降级价格
        /// </summary>
        /// <param name="eqm"></param>
        /// <returns></returns>
        public static long getDowngradePrice(Entities.Usr_Equipment eqm)
        {
            return getUpgradePrice(eqm) / 2;
        }

        /// <summary>
        /// 获取陈功率
        /// </summary>
        /// <returns></returns>
        public static long getSuccessRate()
        {
            // 现在的时间

            return getSuccessRate(GlobalTimer.Instance.GetGlobalTime());
        }

        private static long getSuccessRate(DateTime currentdate)
        {
            long rangeIndex = getSuccessRateRangeIndex(currentdate);

            Random random = new Random();

            // 计算区间值

            switch (rangeIndex)
            {
                case 0:
                    return (long)Math.Ceiling((double)random.Next(10, 35));
                case 1:
                case 7:
                    return (long)Math.Ceiling((double)random.Next(36, 60));
                case 2:
                case 6:
                    return (long)Math.Ceiling((double)random.Next(61, 80));
                case 3:
                case 5:
                    return (long)Math.Ceiling((double)random.Next(81, 90));
                case 4:
                    return (long)Math.Ceiling((double)random.Next(91, 100));

                default:
                    return 0;
            }
        }

        private static long getSuccessRateRangeIndex(DateTime currentdate)
        {
            // 初始化时间 20 = 开始刷新的时间 = 2010年第一个20分钟

            DateTime startdate = DateTimeCoder.Instance.GetFirstDayOfCurrentYear().AddMinutes(successrate_start_delta);

            // 计算现在的区间位置

            TimeSpan timeSpan = (currentdate - startdate);

            long minuteSpan = timeSpan.Minutes;

            long rangeIndex = Math.Abs((minuteSpan / successrate_span) % successrate_period);

            return rangeIndex;
        }

        /// <summary>
        /// 装备强化率是否刷新
        /// </summary>
        /// <param name="lastdate"></param>
        /// <returns></returns>
        public static bool isSuccessRateRefresh(DateTime lastdate)
        {
            return getSuccessRateRangeIndex(lastdate) != getSuccessRateRangeIndex(GlobalTimer.Instance.GetGlobalTime());
        }

        /// <summary>
        /// 成功率趋势 true = 上升
        /// </summary>
        /// <returns></returns>
        public static bool getSucceedTrend()
        {
            long currentRate = getSuccessRate();

            long nextRate = getSuccessRate(GlobalTimer.Instance.GetGlobalTime().AddMinutes(30));

            return nextRate > currentRate;
        }

        /// <summary>
        /// 装备升级
        /// </summary>
        /// <param name="eqm"></param>
        /// <param name="succeedrate"></param>
        /// <returns></returns>
        public static bool upgrade(Pixysoft.Weblications.Sango.Entities.Usr_Equipment eqm, long succeedrate)
        {
            if (new Random().Next(0, 100) > succeedrate)
                return false;

            Entities.Eqm_Grade grade = EqmGradeHelper.search(eqm.Equipmenttypecode, eqm.Gradetypecode);

            eqm.Equipmentpointvalue = (eqm.Equipmentpointvalue + grade.Upgradepoint);

            eqm.Gradelevel = (eqm.Gradelevel + 1);

            return true;
        }

        /// <summary>
        /// 仓库位消耗金币
        /// </summary>
        /// <param name="currentStorageCount"></param>
        /// <returns></returns>
        public static long getNextStoragePrice(long currentStorageCount)
        {
            //50金币开始，每增加一个+50，1000金币封顶 = 20个。

            long incCount = currentStorageCount - PlayerFormular.default_storagecount + 1;

            long goldprice = 50 * incCount;

            if (goldprice > 1000)
                goldprice = 1000;

            return goldprice;
        }

        /// <summary>
        /// 装备销售价格
        /// </summary>
        /// <param name="syseqm"></param>
        /// <param name="eqm"></param>
        /// <returns></returns>
        public static long getSellprice(Pixysoft.Weblications.Sango.Entities.Eqm_Equipment syseqm, Pixysoft.Weblications.Sango.Entities.Usr_Equipment eqm)
        {
            Entities.Eqm_Equipment sysEqm = EqmEquipmentHelper.searchBySerialcode(eqm.Equipmentserialcode);

            double buyprice = getEquipmentCashprice(sysEqm);

            double upgradeprice = (149 + 51 * eqm.Gradelevel) * (eqm.Gradelevel - 1) / 2;

            return (long)Math.Round((buyprice + upgradeprice) / 2);
        }
    }
}
